$(document).ready(function(){
    $('.sym_languages_admin .checkbox')
    .button()
    .click(function(){
        var id=$(this).parent().find('.radio').val();
        enabled=$(this).attr('checked')?1:0;
        $.ajax({
            data:{
                'symbiont': 'languages.db_enabled',
                'id': id,
                'enabled': enabled
            }
        });
    });
    $('.sym_languages_admin .radio')
    .button()
    .click(function(){
        var id=$(this).val();
        $.ajax({
            data:{
                'symbiont': 'languages.db_default',
                'id': id
            }
        });
    });
    $('.sym_languages_admin ul').sortable({
        handle: '.sort',
        placeholder: 'ui-state-highlight',
        update: function(){
            var sort=new Array();
            $('.sym_languages_admin .radio').each(function(){
                sort.push($(this).val());
            });
            $.ajax({
                data:{
                    'symbiont': 'languages.db_sort',
                    'sort': sort
                }
            });
        }
    });
    //Add
    $('.sym_languages_admin .symbiosis-button-add').click(function(){
        if($('#sym_languages_admin_add').length){
            $('#sym_languages_admin_add').dialog('open');
        }
        else{
            $('#windows').append('<div id="sym_languages_admin_add" title="Lanuage adding"></div>');
            $('#sym_languages_admin_add').dialog({
                modal: true,
                width: 270,
                position: ['center', 100],
                open: function(event, ui) {
                    $.ajax({
                        data:{
                            symbiont: 'languages.add'
                        },
                        success:function(r){
                            $('#sym_languages_admin_add').html(r).sym();
                        },
                        dataType: 'html'
                    });
                },
                buttons:{
                    "Cancel":function(){
                        $(this).dialog("close");
                    },
                    "Ok":function(){
                        var abbr=$('#sym_languages_admin_add .abbr').val();
                        var title=$('#sym_languages_admin_add .title').val();
                        var title_en=$('#sym_languages_admin_add .title_en').val();
                        var d=$(this);
                        $.ajax({
                            data:{
                                symbiont: 'languages.db_add',
                                abbr: abbr,
                                title: title,
                                title_en: title_en
                            },
                            success:function(r){
                                d.dialog("close");
                                location.reload(true);
                            }
                        });
                    }
                }
            });
        }
    });
    //Edit
    $('.sym_languages_admin .edit').click(function(){
        t=this;
        if($('#sym_languages_admin_edit').length){
            $('#sym_languages_admin_edit').dialog('open');
        }
        else{
            $('#windows').append('<div id="sym_languages_admin_edit" title="Lanuage editing"></div>');
            $('#sym_languages_admin_edit').dialog({
                modal: true,
                width: 270,
                position: ['center', 100],
                open: function(event, ui) {
                    var id=$(t).parent().find('.radio').val();
                    $.ajax({
                        data:{
                            symbiont: 'languages.edit.'+id
                        },
                        success:function(r){
                            $('#sym_languages_admin_edit').html(r).sym();
                        },
                        dataType: 'html'
                    });
                },
                buttons:{
                    "Cancel":function(){
                        $(this).dialog("close");
                    },
                    "Ok":function(){
                        var id=$('#sym_languages_admin_edit .id').val();
                        var abbr=$('#sym_languages_admin_edit .abbr').val();
                        var title=$('#sym_languages_admin_edit .title').val();
                        var title_en=$('#sym_languages_admin_edit .title_en').val();
                        var d=$(this);
                        $.ajax({
                            data:{
                                symbiont: 'languages.db_edit',
                                id: id,
                                abbr: abbr,
                                title: title,
                                title_en: title_en
                            },
                            success:function(r){
                                d.dialog("close");
                                location.reload(true);
                            }
                        });
                    }
                }
            });
        }
    });
    //Delete
    $('.sym_languages_admin .delete').click(function(){
        t=$(this);
        if($('#sym_languages_admin_delete').length){
            $('#sym_languages_admin_delete').dialog('open');
        }
        else{
            $('#windows').append('<div id="sym_languages_admin_delete" title="Lanuage deleting"></div>');
            $('#sym_languages_admin_delete').dialog({
                modal: true,
                width: 270,
                position: ['center', 100],
                open: function(event, ui) {
                    var id=$(t).parent().find('.radio').val();
                    $.ajax({
                        data:{
                            symbiont: 'languages.delete.'+id
                        },
                        success:function(r){
                            $('#sym_languages_admin_delete').html(r).sym();
                        },
                        dataType: 'html'
                    });
                },
                buttons:{
                    "Cancel":function(){
                        $(this).dialog("close");
                    },
                    "Ok":function(){
                        var id=t.parent().find('.radio').val();
                        var d=$(this);
                        $.ajax({
                            data:{
                                symbiont: 'languages.db_delete',
                                id: id
                            },
                            success:function(r){
                                d.dialog("close");
                                t.parent().slideUp();
                            }
                        });
                    }
                }
            });
        }
    });
});